package site.marqstree.latte.ec.main.cart;

import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.core.content.ContextCompat;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.joanzapata.iconify.widget.IconTextView;
import java.util.List;
import site.marqstree.latte.core.app.Latte;
import site.marqstree.latte.ec.R;
import site.marqstree.latte.ui.recycler.MultipleFields;
import site.marqstree.latte.ui.recycler.MultipleItemEntity;

/*
 * 项目名: EleCommerce
 * 包名: site.marqstree.latte.ec.main.cart
 * 文件名: ShopCartAdapter
 * 创建者: marqstree
 * 创建时间: 2020/2/1 15:29
 * 描述: TODO
 */
public final class ShopCartAdapter extends BaseMultiItemQuickAdapter<MultipleItemEntity, BaseViewHolder> {

    //标识只刷新Item的勾选子控件
    public static int ITEM_SELECTED_PAYLOAD =901;
    //标识只刷新Item的数量子控件
    public static int ITEM_COUNT_PAYLOAD = 902;

    //购物车总价
    private double mTotalPrice = 0.00;

    public void setTotalPriceListener(ITotalPriceListener mTotalPriceListener) {
        this.mTotalPriceListener = mTotalPriceListener;
    }

    private ITotalPriceListener mTotalPriceListener;

    private static final RequestOptions OPTIONS = new RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .centerCrop()
            .dontAnimate();

    ShopCartAdapter(List<MultipleItemEntity> data) {
        super(data);

        //初始化总价
        calTotalPrice();

        //添加购物测item布局
        addItemType(ShopCartItemType.SHOP_CART_ITEM, R.layout.item_shop_cart);
    }

    //当无 payload info 时，只会执行此方法
    //更新Item中的所有子控件
    @Override
    protected void convert(BaseViewHolder holder, MultipleItemEntity entity) {
        switch (holder.getItemViewType()) {
            case ShopCartItemType.SHOP_CART_ITEM:
                //先取出所有值
                final int id = entity.getField(MultipleFields.ID);
                final String thumb = entity.getField(MultipleFields.IMAGE_URL);
                final String title = entity.getField(ShopCartItemFields.TITLE);
                final String desc = entity.getField(ShopCartItemFields.DESC);
                final int count = entity.getField(ShopCartItemFields.COUNT);
                final double price = entity.getField(ShopCartItemFields.PRICE);
                //在左侧勾勾渲染之前改变全选与否状态
                //entity.setField(ShopCartItemFields.IS_SELECTED, mIsSelectedAll);
                final boolean isSelected = entity.getField(ShopCartItemFields.IS_SELECTED);
                //取出所以控件
                final ImageView imgThumb = holder.getView(R.id.image_item_shop_cart);
                final TextView tvTitle = holder.getView(R.id.tv_item_shop_cart_title);
                final TextView tvDesc = holder.getView(R.id.tv_item_shop_cart_desc);
                final TextView tvPrice = holder.getView(R.id.tv_item_shop_cart_price);
                final IconTextView iconMinus = holder.getView(R.id.icon_item_minus);
                final IconTextView iconPlus = holder.getView(R.id.icon_item_plus);
                final TextView tvCount = holder.getView(R.id.tv_item_shop_cart_count);
                final IconTextView iconIsSelected = holder.getView(R.id.icon_item_shop_cart);

                //赋值
                tvTitle.setText(title);
                tvDesc.setText(desc);
                tvPrice.setText(String.valueOf(price));
                tvCount.setText(String.valueOf(count));
                Glide.with(Latte.getApplicationContext())
                        .load(thumb)
                        .apply(OPTIONS)
                        .into(imgThumb);

                //根据数据状态显示左侧勾勾
                if (isSelected) {
                    iconIsSelected.setTextColor
                            (ContextCompat.getColor(Latte.getApplicationContext(), R.color.app_main));
                } else {
                    iconIsSelected.setTextColor(Color.GRAY);
                }
                break;
            default:
                break;
        }
    }

    //当有 payload info 时，只会执行此方法
    //只更新Item中的部分子控件
    @Override
    protected void convert(BaseViewHolder holder, MultipleItemEntity entity, List<?> payloads) {

        for (Object p : payloads) {
            int payload = (int) p;
            //标识只更新RecyclerView的Item中的勾选子控件，不更新图片等其它子控件
            if (payload == ITEM_SELECTED_PAYLOAD) {
                final boolean isSelected = entity.getField(ShopCartItemFields.IS_SELECTED);
                final IconTextView iconIsSelected = holder.getView(R.id.icon_item_shop_cart);
                //根据数据状态显示左侧勾勾
                if (isSelected) {
                    iconIsSelected.setTextColor
                            (ContextCompat.getColor(Latte.getApplicationContext(), R.color.app_main));
                } else {
                    iconIsSelected.setTextColor(Color.GRAY);
                }
            }else if(payload == ITEM_COUNT_PAYLOAD){
                final int count = entity.getField(ShopCartItemFields.COUNT);
                final TextView tvCount = holder.getView(R.id.tv_item_shop_cart_count);
                tvCount.setText(count+"");
            }
        }
    }

    public double getTotalPrice() {
        return mTotalPrice;
    }

    public void setTotalPrice(double mTotalPrice) {
        this.mTotalPrice = mTotalPrice;
        if (mTotalPriceListener != null) {
            mTotalPriceListener.onTatalPriceChange(mTotalPrice);
        }
    }

    //计算总价
    public double calTotalPrice(){
        List<MultipleItemEntity> listData = getData();
        double totalPrice = 0.0;
        if(listData!=null&& listData.size()>0) {
            for (MultipleItemEntity entity : listData) {
                int cnt = entity.getField(ShopCartItemFields.COUNT);
                double price = entity.getField(ShopCartItemFields.PRICE);
                totalPrice = cnt * price + totalPrice;
            }
        }
        setTotalPrice(totalPrice);

        return totalPrice;
    }

    //计算总价(防止异步调用getData，拿不到最新数据)
    public double calTotalPrice(List<MultipleItemEntity> listData){
        double totalPrice = 0.0;
        if(listData!=null&& listData.size()>0) {
            for (MultipleItemEntity entity : listData) {
                int cnt = entity.getField(ShopCartItemFields.COUNT);
                double price = entity.getField(ShopCartItemFields.PRICE);
                totalPrice = cnt * price + totalPrice;
            }
        }
        setTotalPrice(totalPrice);

        return totalPrice;
    }
}
